﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bintje.bintje;

namespace Bintje
{
    public class WayPoint : GeoLocation
    {
        #region Fields

        private IList<Annotation> m_annotations;
        private long m_created1;
        private IList<Image> m_images;
        private string m_internalImageIds;
        private DateTime m_modified;
        private bool m_modifiedSpecified;
        private string m_name;
        private int m_userId;
        private string m_description;
        private string[] m_imageUrl;
        private string m_city;
        private string m_country;
        private string m_latitude;
        private string m_longitude;
        private int m_number;
        private string m_numberAddendum;
        private string m_province;
        private string m_street;
        private string m_zipcode;

        #endregion
        
        #region Properties

        public int UserId
        {
            get { return m_userId; }
            set { m_userId = value; }
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public bool ModifiedSpecified
        {
            get { return m_modifiedSpecified; }
            set { m_modifiedSpecified = value; }
        }

        public DateTime Modified
        {
            get { return m_modified; }
            set { m_modified = value; }
        }

        

        public IList<Annotation> Annotations
        {
            get { return m_annotations; }
            set { m_annotations = value; }
        }

        public IList<Image> Images
        {
            get { return m_images; }
            set { m_images = value; }
        }

        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        public string[] ImageUrl
        {
            get { return m_imageUrl; }
        }

        public string Zipcode
        {
            get { return m_zipcode; }
            set { m_zipcode = value; }
        }

        public string Street
        {
            get { return m_street; }
            set { m_street = value; }
        }

        public string Province
        {
            get { return m_province; }
            set { m_province = value; }
        }

        public string NumberAddendum
        {
            get { return m_numberAddendum; }
            set { m_numberAddendum = value; }
        }

        public int Number
        {
            get { return m_number; }
            set { m_number = value; }
        }

        public string Longitude
        {
            get { return m_longitude; }
            set { m_longitude = value; }
        }

        public string Latitude
        {
            get { return m_latitude; }
            set { m_latitude = value; }
        }

        public string Country
        {
            get { return m_country; }
            set { m_country = value; }
        }

        public string City
        {
            get { return m_city; }
            set { m_city = value; }
        }

        public long Created1
        {
            get { return m_created1; }
            set { m_created1 = value; }
        }

        #endregion

        #region ctor

        public WayPoint()
        {
        }

        public WayPoint(waypoint waypoint)
        {
            if (null != waypoint.annotations && waypoint.annotations.Length > 0)
            {
                m_annotations = new List<Annotation>();
                for (int i = 0; i < waypoint.annotations.Length; i++)
                {
                    m_annotations.Add(new Annotation(waypoint.annotations[i]));
                }
            }

            m_id = waypoint.id;

            if (null != waypoint.images && waypoint.images.Length > 0)
            {
                m_images = new List<Image>();
                for (int i = 0; i < waypoint.images.Length; i++)
                {
                    m_images.Add(new Image(waypoint.images[i]));
                }
            }

            m_internalImageIds = waypoint.internalImageIds;
            m_name = waypoint.name;
            m_userId = waypoint.userId;
            m_description = waypoint.description;
        }

        public WayPoint(waypoint waypoint, string[] urls)
        {
            if (null != waypoint.annotations && waypoint.annotations.Length > 0)
            {
                m_annotations = new List<Annotation>();
                for (int i = 0; i < waypoint.annotations.Length; i++)
                {
                    m_annotations.Add(new Annotation(waypoint.annotations[i]));
                }
            }

            m_id = waypoint.id;

            if (null != waypoint.images && waypoint.images.Length > 0)
            {
                m_images = new List<Image>();
                for (int i = 0; i < waypoint.images.Length; i++)
                {
                    m_images.Add(new Image(waypoint.images[i], urls[i]));
                }
            }

            m_imageUrl = urls;
            m_internalImageIds = waypoint.internalImageIds;
            m_name = waypoint.name;
            m_userId = waypoint.userId;
            m_description = waypoint.description;
        } 

        #endregion

        #region Methods

        public bool Create()
        {
            waypointRequest request = GetRequest();
            statusResponse response = API.instance.createWaypoint(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                return true;
            }
            return false;
        }

        public bool Delete()
        {
            waypointRequest request = GetRequest();
            statusResponse response = API.instance.deleteWaypoint(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                return true;
            }
            return false;
        }

        public bool Update()
        {
            waypointRequest request = GetRequest();
            statusResponse response = API.instance.updateWaypoint(request);
            if (null != response && response.status == API.RESPONSE_SUCCESS)
            {
                return true;
            }
            return false;
        }

        private waypointRequest GetRequest()
        {
            waypoint waypoint = ConvertToAPI();
            waypointRequest request = new waypointRequest();
            request.waypoint = waypoint;
            return request;
        }
        
        internal waypoint ConvertToAPI()
        {
            waypoint waypoint = new waypoint();

            if (null != this.Annotations && this.Annotations.Count > 0)
            {
                waypoint.annotations = Annotation.ConvertToAPI(this.Annotations);
            }

            waypoint.id = this.Id;

            if (null != this.Images && this.Images.Count > 0)
            {
                waypoint.images = Image.ConvertToAPI(this.Images);
            }

            waypoint.internalImageIds = this.m_internalImageIds;
            waypoint.name = this.Name;
            waypoint.userId = this.UserId;
            waypoint.description = this.Description;

            return waypoint;
        }

        internal static waypoint[] ConvertToAPI(IList<WayPoint> waypointsList)
        {
            waypoint[] waypoints = (waypoint[])Array.CreateInstance(typeof(waypoint), waypointsList.Count);
            for (int i = 0; i < waypointsList.Count; i++)
            {
                waypoints[i] = waypointsList[i].ConvertToAPI();
            }
            return waypoints;
        } 

        #endregion
    }
}
